package cn.tedu.boot22.controller;

import cn.tedu.boot22.utils.DBUtils;
import cn.tedu.boot22.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.*;
import java.util.ArrayList;

@Controller
public class UserController {
    @RequestMapping("/reg")
    @ResponseBody
    public String reg(User user){
        System.out.println("User:"+user);
        //得到连接
        try (Connection conn = DBUtils.getConn()){
            String sql = "select id from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ResultSet rs = ps.executeQuery();
            if(rs.next()){//如果返回true代表数据库中存在这个用户了
                return "用户名已存在";
            }
                String insertSql = "insert into user values(null,?,?,?)";
                PreparedStatement insertPs = conn.prepareStatement(insertSql);
                insertPs.setString(1, user.getUsername());
                insertPs.setString(2, user.getPassword());
                insertPs.setString(3, user.getNick());
                insertPs.executeUpdate();
                System.out.println("注册成功!");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "注册成功";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(User user){
        try (Connection conn = DBUtils.getConn()){
            String sql = "select password from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                //得到查询回来的密码
                String pw = rs.getString(1);
                if(pw.equals(user.getPassword())){
                    return "登录成功!";
                }
                return "密码错误!";
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "用户名不存在!";
    }

    @RequestMapping("/showAllUser")
    @ResponseBody
    public String showAllUser(){
        ArrayList<User> list = new ArrayList<>();
        try (Connection conn = DBUtils.getConn()){
            String sql = "select username,password,nick from user";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                String username = rs.getString(1);
                String password = rs.getString(2);
                String nick = rs.getString(3);
                User user = new User(username,password,nick);
                list.add(user);//把查询到的对象装进集合
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //遍历集合把里面的数据装进一个html里面
        String html = "<table border=1>";
        html+="<caption>用户列表</caption>";
        html+="<tr><th>用户名</th><th>密码</th><th>昵称</th></tr>";
        for (User user: list) {
            html+="<tr>";
            html+="<td>"+user.getUsername()+"</td>";
            html+="<td>"+user.getPassword()+"</td>";
            html+="<td>"+user.getNick()+"</td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
}
